package com.itheima.wemedia.controller;


import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "ApiController" ,tags = "频道列表")
public class ApiController {

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private ApiService apiService;

    @Autowired
    private WmNewsService wmNewsService;


    @ApiOperation("调用admin微服务，查询所有频道列表，优先从redis查")
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channelList(){

        List<AdChannel> channelList = apiService.getChannelList();
        return ResultVo.ok(channelList);

    }

    /**
     * 添加素材
     */
    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.save(wmMaterial);
        return ResultVo.ok();
    }


    /**
     * 通过文章查询id
     * @param newId
     * @return
     */
    @GetMapping("/api/wmNews/{newsId}")
    public ResultVo<WmNews> getWmNewsById(@PathVariable (name = "newsId") Integer newId){
        WmNews wmNews = wmNewsService.getById(newId);
         return ResultVo.ok(wmNews);

    }


    /**
     * 修改文章状态
     * @param wmNews
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo<WmNews> update(@RequestBody WmNews wmNews){

        wmNewsService.updateById(wmNews);
        return ResultVo.ok();

    }
}
